/**
 * The Community Project
 * Copyright Team1 Productions
 * 
 * Joshua Amick
 * Stephen Cunningham
 * Thema Davis
 * Tomika Dawson
 * Lucie DeCristofaro
 * 
 * All Rights Reserved.
 */

package com.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Timer;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Provider;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.dao.EmailDAO;
import com.dao.UserDAO;
import com.domain.Email;
import com.domain.User;

public class CommunityEmailer {
	
	private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final int SMTP_HOST_PORT = 465;
    private static final String SMTP_AUTH_USER = "csce492.community@gmail.com";
    private static final String SMTP_AUTH_PWD  = "community";
    
    public List<Long> emailIds = new ArrayList();
    
    private EmailDAO emailDAO;
    private UserDAO userDAO;
    
    public CommunityEmailer(){}

    public EmailDAO getEmailDAO() {
		return emailDAO;
	}

	public void setEmailDAO(EmailDAO emailDAO) {
		this.emailDAO = emailDAO;
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void sendMail(String emailAddressToSendTo,String subject, String messageText) throws Exception{
        Properties props = new Properties();

        props.put("mail.transport.protocol", "smtps");
        props.put("mail.smtps.host", SMTP_HOST_NAME);
        props.put("mail.smtps.auth", "true");
        // props.put("mail.smtps.quitwait", "false");

        Session mailSession = Session.getDefaultInstance(props);
        mailSession.setDebug(true);
        Transport transport = mailSession.getTransport();
        

        MimeMessage message = new MimeMessage(mailSession);
        message.setSubject(subject);
        message.setContent(messageText, "text/plain");

        message.addRecipient(Message.RecipientType.TO,
             new InternetAddress(emailAddressToSendTo));
        
        Address from = new InternetAddress(SMTP_AUTH_USER);
        Address [] fromArray = new Address [1];
        fromArray[0] = from;
        message.addFrom(fromArray);
        message.setReplyTo(fromArray);
        message.setSender(from);

        transport.connect
          (SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
        

        transport.sendMessage(message,
            message.getRecipients(Message.RecipientType.TO));
        transport.close();
    }
    
    public void addEmail(Long emailId){
    	this.emailIds.add(emailId);
    }

	public void sendMassEmails() {
		if(this.emailIds!=null&&this.emailIds.size()>0){
			Timer timer = new Timer();
			List<Email> emailsToSend = new ArrayList();
			for(Long i:emailIds){
				Email email = this.emailDAO.getEmailByKeyId(i);
				if(email!=null)emailsToSend.add(email);
			}
			List<User> users = this.userDAO.getAllEmailUsers();
			for(User user:users){
				for(Email em:emailsToSend){
					try{
						this.sendMail(user.getEmail(), em.getEmailSubject(), em.getEmailMessage());
						timer.wait(Long.parseLong("500"));
						System.out.println("SENDING EMAIL>>>............");
					}catch(Exception e){
						System.out.println("EMAIL EXCEPTION:  "+e);
					}
				}
			}
			this.emailIds = new ArrayList();
		}
	}
	
}
